package com.zhj.micro.product.consumer;

import com.rabbitmq.client.Channel;
import com.zhj.micro.product.MicroProductApplication;
import com.zhj.micro.product.entity.Order;
import com.zhj.micro.product.service.ProductStorageService;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

import javax.annotation.Resource;

@Component
public class ProductConsumer {

    @Resource
    private ProductStorageService productStorageService;

    @RabbitHandler
    @RabbitListener(queues = "product_queue")
    public void process(Message message, Channel channel) {
        try {
            Order order = MicroProductApplication.OBJECT_MAPPER.readValue(message.getBody(), Order.class);
            //处理库存
            this.productStorageService.processOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (IOException e) {
            System.out.println("订单处理失败");
        }
    }

}
